package com.oocl.servlet.action;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.pojo.CartItem;

@WebServlet("/makesession")
public class MakeSessionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public MakeSessionServlet() {
        super();

    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");                      //解决中文乱码
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("content-type","text/html;charset=UTF-8");//解决中文乱码
		Map<String, CartItem> cart;
		PrintWriter out = response.getWriter();
		if(request.getSession().getAttribute("cart")==null){
			cart = new ConcurrentHashMap<String, CartItem>();
			request.getSession().setAttribute("cart", cart);
		}
		out.println("add cart to session successfully");
		cart = (Map<String, CartItem>)request.getSession().getAttribute("cart");
		for(String did:cart.keySet()){
			CartItem ci = cart.get(did);
			out.println(ci.getdId()+"..."+ci.getdName()+"..."+ci.getPrice()+"..."+ci.getCount());
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

}
